<?php
/**
 * role control
 * milloc
 */
use yii\helpers\Url;

$this->title = '角色管理';
?>
<div class="main-title">
    <div class="title-txt"><?= $this->title ?></div>
</div>
<!--add-->
<div class="panel panel-frame panel-operat">
    <div class="panel-body">
        <div class="panel-content">
            <a href="javascript:onAddRole();" type="button" class="btn btn-primary add-icon ">
                <div class="btn-icon"></div>
                <div class="btn-text">添加</div>
            </a>
        </div>
    </div>
</div>
<!-- end add -->
<!--list-->
<div class="panel panel-frame">
    <div class="table-responsive">
        <table class="table table-bordered table-hover">
            <thead>
            <tr>
                <th>名称</th>
                <th>描述</th>
                <th>是否启用</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
                <?php foreach ($role_list as $role) { ?>
                <tr>
                    <td><?=$role['name']?></td>
                    <td><?=$role['description']?></td>
                    <td>
                        <span class="label label-<?= $role['is_enabled'] ? 'success' : 'danger'?>">
                            <?= $role['is_enabled'] ? '已启用' : '已停用' ?>
                        </span>
                    </td>
                    <td class="table-text-align-left">
                        <a href="javascript:onEditFunctionPoint('<?=$role['id']?>');" class="btn btn-link">编辑功能点</a>
                        <a href="javascript:onAddRole('<?=$role['id']?>');" class="btn btn-link">编辑</a>
                        <a href="javascript:onDeleteRole('<?=$role['id']?>');" class="btn btn-link">删除</a>
                        <a href="<?= Url::toRoute(['manager', 'role_id' => $role['id']]) ?>" class="btn btn-link">查看角色成员</a>
                    </td>
                </tr>
                <?php } ?>
            </tbody>
        </table>
    </div>
</div>
<!-- end list -->
<!--modal-->
<!-- add function point -->
<div class="modal fade" id="modalEditFunctionPoint" tabindex="-1" role="dialog" aria-labelledby="modalEditFunctionPointLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="modalEditFunctionPointLabel">编辑功能点</h4>
            </div>
            <div id="modalEditFunctionPointBody" class="modal-body">
            </div>
            <div class="modal-footer">
                <a class="btn btn-primary" onclick="saveFunctionPoint()"><i class="fa fa-save"></i> 保存
                </a>
                <a class="btn btn-cancel" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </a>
            </div>
        </div>
    </div>
</div>
<!-- end add function point -->
<!-- add|edit role -->
<div class="modal fade" id="modalAddRole" tabindex="-1" role="dialog" aria-labelledby="modalAddRoleLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button class="close" type="button" data-dismiss="modal" aria-label="close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="modalAddRoleLabel">编辑角色</h4>
            </div>
            <div id="modalAddRoleBody" class="modal-body"></div>
            <div class="modal-footer">
                <a onclick="addRole()" class="btn btn-primary">
                    <i class="fa fa-save"> 保存 </i>
                </a>
                <a class="btn btn-primary" data-dismiss="modal">
                    <i class="fa fa-remove"> 取消 </i>
                </a>
            </div>
        </div>
    </div>
</div>
<!-- end add|edit role -->
<!-- end modal -->
<script type="text/javascript">
    function onAddRole(id)
    {
        $('#modalAddRole').modal();
        $('#modalAddRoleBody').html('正在加载数据');
        requestHtml("<?= Url::toRoute('add-role') ?>", {id: id}, true , function (res) {
            $('#modalAddRoleBody').html(res);
        });   
    }

    function addRole()
    {
        var form = $('#formAddRole');
        var name = form.find('#role-name').val();
        var description = form.find('#role-description').val();
        var is_enabled = form.find('#role-is_enabled').val();
        var mess = '';
        if (!name) {
            mess = '名称不能为空';
        } else if (name.length >= 255) {
            mess = '名称不能多于255个字符'
        } else if (!description) {
            mess = '描述不能为空';
        } else if (isNaN(parseInt(is_enabled))) {
            mess = '请选择是否启用';
        }
        if (mess) {
            warningDialog(mess);
            return;
        }

        requestJson("<?= Url::toRoute('save-role') ?>", $('#formAddRole').serialize(), '', function (res) {
            if (res.status == 0) {
                history.go(0);
            } else {
                errorMessage(res.message);
            }
        });
    }

    function saveFunctionPoint()
    {
        var function_point_list = [];
        $('#tableEditRoleFunctionPoint').find(':checkbox:checked').each(function () {
            function_point_list.push($(this).val());
        });
        var id = $('#role_id').val();
        requestJson("<?= Url::toRoute('save-role-function-point') ?>", {function_point_list: function_point_list, role_id: id}, '',function (res) {
            if (res.status == 0) {
                $('#modalEditFunctionPoint').modal('hide');
            } else {
                errorMessage(res.message);
            }
        });
    }

    function onEditFunctionPoint(id)
    {
        $('#modalEditFunctionPoint').modal();
        $('#modalEditFunctionPointBody').html('正在加载数据');
        requestHtml('<?= Url::toRoute('edit-role-function-point') ?>', {id: id}, true, function (res) {
            $('#modalEditFunctionPointBody').html(res);
        });
    }

    function onDeleteRole(id)
    {
        confirmDialog(id, '', function (id) {
            requestJson('delete-role', {id: id}, '', function (res) {
                if (res.status == 0) {
                    history.go(0);
                } else {
                    errorMessage(res.message);
                }
            });
        });
    }
</script>